package com.woyao.core.util;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.woyao.R;
import com.woyao.core.AreaLoader;
import com.woyao.core.model.Area;
import com.woyao.core.model.AreaResponse;

import java.util.ArrayList;

/**
 * Created by summerwind on 2016-06-01.
 */
public class AreaSelect {
    private Activity context;
    private LinearLayout parent;
    private int province;
    private int city;
    private int district;
    private boolean[] used = new boolean[3];

    private Spinner provinceSelect;
    private Spinner citySelect;
    private Spinner districtSelect;

    private ArrayList<Area> provinces;
    private ArrayList<Area> cities;
    private ArrayList<Area> districts;

    private Area none = new Area();

    public void construct(){
        for(int index = 0;index<3;index++){
            used[index] = true;
        }
        if (province > 0) {
            used[0] = false;
        }
        if(city>0){
            used[1] = false;
        }
        if(district>0){
            used[2] = false;
        }
        none.setId(0);
        none.setName("请选择");
        provinceSelect = new Spinner(context);
        citySelect = new Spinner(context);
        districtSelect = new Spinner(context);

        provinceSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<Area> areaList = provinces;
                if(position>0){
                    AreaLoader loader = new AreaLoader(context, areaList.get(position).getId()+"");
                    loader.setOnSuccessListener(new AreaLoader.OnSuccessListener() {
                        @Override
                        public void onSuccess(AreaResponse response) {
                            if(response != null&&response.getContent()!=null){
                                cities = response.getContent();
                                paintArea(2);
                            }else{
                                cities = new ArrayList<Area>();
                                paintArea(2);
                            }
                        }
                    });
                    loader.load();
                }else{
                    cities = new ArrayList<Area>();
                    paintArea(2);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        citySelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<Area> areaList = cities;
                if(position>0){
                    AreaLoader loader = new AreaLoader(context, areaList.get(position).getId()+"");
                    loader.setOnSuccessListener(new AreaLoader.OnSuccessListener() {
                        @Override
                        public void onSuccess(AreaResponse response) {
                            if(response != null&&response.getContent()!=null){
                                districts = response.getContent();
                                paintArea(3);
                            }else{
                                districts = new ArrayList<Area>();
                                paintArea(3);
                            }
                        }
                    });
                    loader.load();
                }else{
                    districts = new ArrayList<Area>();
                    paintArea(3);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        districtSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        parent.addView(provinceSelect,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        parent.addView(citySelect,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        parent.addView(districtSelect,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

        AreaLoader loader = new AreaLoader(context, "");
        loader.setOnSuccessListener(new AreaLoader.OnSuccessListener() {
            @Override
            public void onSuccess(AreaResponse response) {
                if(response != null){
                    provinces = response.getContent();
                    paintArea(1);
                }
            }
        });
        loader.load();
    }

    private void paintArea(int level){

        Spinner spinner;
        ArrayList<Area> arrayList;
        int id;
        switch (level){
            case 1:
                spinner = provinceSelect;
                arrayList = provinces;
                id = province;
                break;
            case 2:
                spinner = citySelect;
                arrayList = cities;
                id = city;
                break;
            case 3:
                spinner = districtSelect;
                arrayList = districts;
                id = district;
                break;
            default:
                return;
        }
        arrayList.add(0, none);
        String[] areas = new String[arrayList.size()];
        for(int index = 0;index<arrayList.size();index++){
            areas[index] = arrayList.get(index).getName();
        }
        spinner.setAdapter(new ArrayAdapter<String>(context,
                R.layout.support_simple_spinner_dropdown_item,
                areas));
        if(!used[level-1]){
            boolean find = false;
            for(int i = 0;i<arrayList.size();i++){
                if(id == arrayList.get(i).getId()){
                    spinner.setSelection(i);
                    find = true;
                    break;
                }
            }
            used[level-1] = true;
            if(!find){
                spinner.setSelection(0);
            }
        }else{
            spinner.setSelection(0);
        }

    }

    public int getId(int level){
        Spinner spinner;
        ArrayList<Area> arrayList;
        switch (level){
            case 1:
                spinner = provinceSelect;
                arrayList = provinces;
                break;
            case 2:
                spinner = citySelect;
                arrayList = cities;
                break;
            case 3:
                spinner = districtSelect;
                arrayList = districts;
                break;
            default:
                return 0;
        }
        int pos = spinner.getSelectedItemPosition();
        if(arrayList.size()>pos){
            return arrayList.get(pos).getId();
        }
        return 0;
    }

    public Activity getContext() {
        return context;
    }

    public void setContext(Activity context) {
        this.context = context;
    }

    public LinearLayout getParent() {
        return parent;
    }

    public void setParent(LinearLayout parent) {
        this.parent = parent;
    }

    public int getProvince() {
        return province;
    }

    public void setProvince(int province) {
        this.province = province;
    }

    public int getCity() {
        return city;
    }

    public void setCity(int city) {
        this.city = city;
    }

    public int getDistrict() {
        return district;
    }

    public void setDistrict(int district) {
        this.district = district;
    }
}
